package edu.boun.g4.coursity.service;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.boun.g4.coursity.domain.Language;

/**
 * 
 * @author mertusta
 * @author alifindik
 * 
 */
@Service("languageService")
@Transactional
public class LanguageService {

	protected static Logger logger = Logger.getLogger("service");

	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;

	/**
	 * Retrieves all languages
	 * 
	 * @return a list of languages
	 */
	@SuppressWarnings("unchecked")
	public List<Language> getAll() {
		logger.debug("Retrieving all languages");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Create a Hibernate query (HQL)
		Query query = session.createQuery("FROM  Language");

		// Retrieve all
		return query.list();
	}

	// /**
	// * Retrieves languages of a course
	// */
	// public List<Language> getAll(Integer courseId) {
	// logger.debug("Retrieving languages of the course");
	//
	// // Retrieve session from Hibernate
	// Session session = sessionFactory.getCurrentSession();
	//
	// // Create a Hibernate query (HQL)
	// Query query = session
	// .createQuery("FROM Course as p LEFT JOIN FETCH  p.languages WHERE p.id="
	// + courseId);
	//
	// Course course = (Course) query.uniqueResult();
	//
	// // Retrieve all
	// return new ArrayList<Language>(course.getLanguages());
	// }

	/**
	 * Retrieves a single language
	 */
	public Language get(Integer id) {
		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing language first
		Language language = (Language) session.get(Language.class, id);

		return language;
	}

	/**
	 * Adds a new language
	 */
	public void add(Language language) {
		logger.debug("Adding new language");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Persists to db
		session.save(language);
	}

	/**
	 * Deletes an existing language
	 * 
	 * @param id
	 *            the id of the existing language
	 */
	public void delete(Integer id) {
		logger.debug("Deleting existing language");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing language first
		Language language = (Language) session.get(Language.class, id);

		// Delete
		session.delete(language);
	}

	/**
	 * Edits an existing initiative
	 */
	public void edit(Language language) {
		logger.debug("Editing existing language");

		// Retrieve session from Hibernate
		Session session = sessionFactory.getCurrentSession();

		// Retrieve existing language via id
		Language existingLanguage = (Language) session.get(Language.class,
				language.getId());

		// Assign updated values to this language
		existingLanguage.setLanguageName(language.getLanguageName());

		// Save updates
		session.save(existingLanguage);
	}
}
